package cn.hxsteel.storage.controller;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FaviconIcoController extends HttpServlet
{
    public FaviconIcoController()
    {
	super();
    }
    
    public void destroy()
    {
	super.destroy();
    }
    
    public void init()
	    throws ServletException
    {
	super.init();
    }
    
    @RequestMapping( "/favicon.ico" )
    public void service( HttpServletRequest request, HttpServletResponse response )
	    throws ServletException, IOException
    {
	response.setContentType("image/ico");
	OutputStream out = response.getOutputStream();
	String path = request.getSession().getServletContext().getRealPath("/resource/images/favicon.ico");
	FileInputStream stream = new FileInputStream(path);
	byte[] buffer = new byte[1024];
	int size;
	while((size = stream.read(buffer)) > -1){
	  out.write(Arrays.copyOfRange(buffer, 0, size));
	}
	out.flush();
	out.close();
    }
}
